package user

import (
	"github.com/gin-gonic/gin"
	"go-server/models"
	"go-server/models/base"
	"go-server/service"
	"net/http"
)

// @Summary   登录接口1
// @description 登录接口2
// @Tags 登录接口3
// @Accept json
// @Produce  json
// @Param models.SysUserLoginDto body models.SysUserLoginDto false "用户名称"
// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
// @failure 500
// @Router /user/login  [post]
func Login(ctx *gin.Context) {

	var sysUserDto models.SysUser
	ctx.ShouldBind(&sysUserDto)
	sysUser, err := service.User.Login(sysUserDto)

	if err != nil {
		ctx.JSON(http.StatusOK, base.R.Error("账户名或者密码错误"))
	} else {
		ctx.JSON(http.StatusOK, base.R.Success(sysUser))
	}
}

func List(ctx *gin.Context) {
	var sysUserListDto models.SysUserListDto
	ctx.ShouldBind(&sysUserListDto)
	sysUsers, err := service.User.List(&sysUserListDto)
	if err != nil {
		ctx.JSON(http.StatusOK, base.R.Error("查询失败"))
	} else {
		ctx.JSON(http.StatusOK, base.R.Success(sysUsers))
	}
}

func Info(ctx *gin.Context) {
	var sysUserInfoDto models.SysUserInfoDto
	ctx.ShouldBindUri(&sysUserInfoDto)
	sysUserInfoVo, err := service.User.Info(sysUserInfoDto.Id)
	if err != nil {
		ctx.JSON(http.StatusOK, base.R.Error("根据id查询失败"))
	} else {
		ctx.JSON(http.StatusOK, base.R.Success(sysUserInfoVo))
	}
}

func Save(ctx *gin.Context) {
	var sysUserSaveDto models.SysUser
	ctx.ShouldBind(&sysUserSaveDto)
	bool := service.User.Save(&sysUserSaveDto)
	if bool {
		ctx.JSON(http.StatusOK, base.R.Success(nil))
	} else {
		ctx.JSON(http.StatusOK, base.R.Error("保存失败"))
	}
}

func Update(ctx *gin.Context) {
	var sysUserUpdateDto models.SysUser
	ctx.ShouldBind(&sysUserUpdateDto)
	bool := service.User.Update(&sysUserUpdateDto)
	if bool {
		ctx.JSON(http.StatusOK, base.R.Success(nil))
	} else {
		ctx.JSON(http.StatusOK, base.R.Error("修改失败"))
	}
}

func Delete(ctx *gin.Context) {
	var sysUserDeleteDto models.SysUserDeleteDto
	ctx.ShouldBindUri(&sysUserDeleteDto)
	bool := service.User.Delete(sysUserDeleteDto.Id)
	if bool {
		ctx.JSON(http.StatusOK, base.R.Success(nil))
	} else {
		ctx.JSON(http.StatusOK, base.R.Error("删除失败"))
	}
}
